<?php
/**
 * Created by PhpStorm.
 * User: bxm
 * Date: 2022/6/28
 * Time: 0:03
 */

namespace app\api\controller\worker;
use app\api\controller\Base;

use app\common\model\node\NodeProcessModel;
use app\common\model\worker\CustomerContractModel;
use app\common\model\worker\CustomerGoodsDataModel;
use app\common\model\worker\TaskModel;
use think\facade\Db;
use think\Request;

class CustomerContractController  extends Base
{
    private $model;
    private $taskModel;
    /**
     * 构造方法
     * @access public
     * @throws \Exception
     */
    public function __construct()
    {

        parent::__construct();
        $this->checkLogin();
        $this->model = new CustomerContractModel();
        $this->taskModel = new TaskModel();

    }
    public function list(){
        $param = $this->request->param();
        $pageSize      = $this->request->param('pageSize/d', 20); //每页多少条
        $pageNumber    = $this->request->param('pageNumber/d') ?? 1; //当前页
        $list = $this->model->alias('cal')
            ->leftJoin('admin a', 'a.id = cal.action_admin_id')
            ->where('cal.customer_id',$param['customer_id'])
            ->order('cal.id','desc')
            ->field('a.nickname,cal.*')
            ->paginate($pageSize, false, ['page' =>$pageNumber ]);

        $this->result($list,'操作成功',1);
    }
    public function add(){
//        $roles_ids = $this->role_ids;
//        if(!in_array(8,$roles_ids)){
//            //exception('该用户没有权限，只有客户代表有操作权限！');
//            $this->result([],'该用户没有权限，只有客户代表有操作权限！',0);
//        }
        $data = $this->request->param();
        $this->validate($data,[
            'customer_id|客户id'=>'require',
            'goods_data_id|商品id'=>'require',
            'goods_name|产品名称'=>'require',
            'contract_imgs|合同'=>'require',
            'price_detail_imgs|价格明细'=>'require',
            'drawing_imgs|图纸'=>'require',
            'sample_imgs|样品'=>'require',
            'notice_imgs|须知'=>'require',
        ]);
        $data = [
            'customer_id' => $data['customer_id'],
             'goods_data_id' => $data['goods_data_id'],
            'goods_name' => $data['goods_name'],
            'expect_time' => $data['expect_time'],
            'retail_price' => $data['retail_price'],
            'discount_price' => $data['discount_price'],
            'final_price'=> $data['final_price'],
            'contract_imgs' => json_encode($data['contract_imgs'],JSON_UNESCAPED_UNICODE),//任务图片
            'price_detail_imgs' => json_encode($data['price_detail_imgs'],JSON_UNESCAPED_UNICODE),//任务图片
            'drawing_imgs' => json_encode($data['drawing_imgs'],JSON_UNESCAPED_UNICODE),//任务图片
            'sample_imgs' => json_encode($data['sample_imgs'],JSON_UNESCAPED_UNICODE),//任务图片
            'notice_imgs' => json_encode($data['notice_imgs'],JSON_UNESCAPED_UNICODE),//任务图片
            'remark' => $data['remark'] ?? '',
            'action_admin_id' =>$this->loginInfo['id'],
        ];
        $customerGoodsDataInfo = CustomerGoodsDataModel::where('id', $data['goods_data_id'])->find();
        if($customerGoodsDataInfo['budget_status'] != 1 && $customerGoodsDataInfo['design_status'] != 3){
            $this->result([],'必须先完成预算和量房后，方可操作签合同！',0);
        }
        if($customerGoodsDataInfo['contract_id']){
            //exception('已签合同，请勿重复操作！');
            $this->result([],'已签合同，请勿重复操作！',0);
        }

        Db::startTrans();
        $info = $this->model->add($data);
        if(!$info){
            // 回滚事务
            Db::rollback();
            $this->result([], "操作失败", 0);
        }
        $nodeProcess = NodeProcessModel::where('node_id', 9)->order('id', 'asc')->find();
        $dataTask = [
            'customer_id' => $data['customer_id'],
            'order_id' => 0,
            'node_id' => 9,
            'process_id' => $nodeProcess['id'],
            'receive_admin_id' => $nodeProcess['audit_admin_id'],
            'action_table_id'=>$info['id'],//操作表id
            'admin_id'=>$this->loginInfo['id']
        ];
        $result = $this->taskModel->commonAddTask($dataTask);
        if(!$result){
            // 回滚事务
            Db::rollback();
            $this->result([], "操作失败", 0);
        }
        Db::commit();
        $this->taskModel->sendTaskInfo($dataTask['receive_admin_id'],$nodeProcess['process_name']);
        $this->result($info,'操作成功',1);
    }
    public function getInfo(Request $request)
    {
        $param = $request->param();
        //开始校验
        $this->validate($param, [
            'id|记录id' => 'require',
        ]);
        $id = $param['id'];
        $list = $this->model->alias('cal')->leftJoin('admin a', 'a.id = cal.action_admin_id')
            ->where('cal.id', $id)
            ->field('a.nickname,cal.*')
            ->find();
        $this->result($list, '操作成功', 1);
    }
}